//package com.activiti.image;
//
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.activiti.bpmn.model.BpmnModel;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.RuntimeService;
//import org.activiti.engine.runtime.ProcessInstance;
//import org.activiti.image.impl.DefaultProcessDiagramGenerator;
//import org.springframework.stereotype.Component;
//
//import java.io.ByteArrayOutputStream;
//import java.io.InputStream;
//import java.util.Base64;
//import java.util.List;
//import java.util.Objects;
//
//@Slf4j
//@Component
//@AllArgsConstructor
//public class FlowImageComponent {
//    private final RepositoryService repositoryService;
//    private final RuntimeService runtimeService;
//
//    /*
//     * 生成流程实例图Base64字符串
//     * -通过老版本的activiti engine中的实例图api获取流程图-
//     *
//     * @param processId 流程实例id
//     */
//    public String generateProcessImage(String processId) {
//        ProcessInstance processInstance = getProcessImage(processId);
//        if (Objects.isNull(processInstance)) {
//            log.warn("未找到对应的流程实例，ID: {}", processId);
//        }
//
//        // 获取bpmn模型
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
//        if (Objects.isNull(bpmnModel)) {
//            log.warn("未找到对应的bpmn模型，流程实例ID: {}", processId);
//        }
//
//        // 获取当前活动节点id(当前正在运行的任务节点)
//        List<String> activeActivityIds = runtimeService.getActiveActivityIds(processId);
//
//        // 生成流程图
//        DefaultProcessDiagramGenerator diagramGenerator = new DefaultProcessDiagramGenerator();
//        InputStream inputStream = diagramGenerator.generateDiagram(
//                bpmnModel,
//                "png",
//                // 高亮节点
//                activeActivityIds
//        );
//        return convertToBase64(inputStream);
//    }
//
//    /*
//     * 将输入流转换为Base64字符串
//     *
//     * @param inputStream 输入流
//     */
//    private String convertToBase64(InputStream inputStream) {
//        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//            byte[] buffer = new byte[1024];
//            int bytesRead;
//            while ((bytesRead = inputStream.read(buffer)) != -1) {
//                outputStream.write(buffer, 0, bytesRead);
//            }
//            return Base64.getEncoder().encodeToString(outputStream.toByteArray());
//        } catch (Exception e) {
//            throw new RuntimeException("生成 Base64 流程图失败", e);
//        }
//    }
//
//    /**
//     * 获取流程实例
//     *
//     * @param processId 流程实例id
//     */
//    private ProcessInstance getProcessImage(String processId) {
//        return runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
//    }
//}
